# Replication File for 
# "Information Credibility and Responses to Corruption: A Replication and Extension in Argentina"
# Political Science Research and Methods
# Matthew S. Winters and Rebecca Weitz-Shapiro

# File Created on 13 July 2018

# This replication file makes use of two data sources.  

# The first  ("Weitz-ShapiroWinters_JOP_Credibility_ReplicationData.dta") is the 
# replication dataset from Weitz-Shapiro and Winters (2017) that is part of the 
# replication package found here: http://dx.doi.org/10.7910/DVN/JOXCZY.  All of 
# the included variables can be found in the .do file at that link.  

# The second ("WintersWeitz-Shapiro_PSRM_Argentina_ReplicationData.dta") is 
# a selection of data from Wave 1 of the Argentina Panel Election Study conducted 
# in July 2015 by MORI Argentina.  For more on that dataset, see the volume by 
# Lupu, Oliveros, and Schiumerini (2019).

# The subset of the APES data in this archive includes the following variables
# (which are the original variables distributed with the APES data and our
# recodings):

# id - survey id
# pm - primary sampling unit
# ciudad - city
# provincia - province
# region - region
# sexo - male=1; female=2
# edad - age
# date - date of survey
# start_time - starting time of survey
# end_time - ending time of survey

# vignette - a 1...12 indicator for the vignette administered to the respondent
 # 1 (clean, no party); 2 (clean, Peronist); 3 (clean, Radical);
 # 4 (NGO accusations, no party); 5 (NGO accusations, Peronist); 6 (NGO accusations, Radical);
 # 7 (opposition party accusations, no party); 8 (Radical accusations, Peronist); 9 (Peronist accusations, Radical);
 # 10 (pure control, no party); 11 (pure control, Peronist); 12 (pure control, Radical)
# cred_vs_less - 0/1 indicator for vignettes 4 through 6 (=1) versus 7 through 9 (=0)
# clean_vs_control - 0/1 indicator for vignettes 1 through 3 (=1) versus 10 through 12 (=0)
# control - 0/1 indicator for vignettes 10 - 13
# clean - 0/1 indicator for vignettes 1 - 3
# bribes_less - 0/1 indicator for vignettes 7 - 9
# bribes_credible - 0/1 indicator for vignettes 4 - 6

# P25 - "Considering what you have heard about the mayor, how likely are you to vote for
 # him in the upcoming election?"
# voteintent - recoded version of P25 - 1 = "I would not vote; not likely; 2 = "not very likely";
 # 3 = "somewhat likely; 4 = "very likely"
# votedich - 0/1 - =1 if voteintent==3 or 4; =0 if voteintent==1 or 2

# P89b - "What is the maximum level of education that you have obtained?"
# education - recoded version of P89b - 1 = No instruction / incomplete primary school;
 # 2 = complete primary school / incomplete secondary school; 3 = completed secondary school;
 # 4 = at least some terciary education

# P72 - "Do you know in what year they approved the law for marriage between people
 # of the same sex?"
# P73 - "Including Buenos Aires, how many provinces are there in Argentina?"
# P74 - "Who is the Minister of Economy for the country?"
# P75 - "Who is the current president of Brazil?"
# P76 - "From what you remember, which of the following laws was reformed in the last
 # four years?"
# P77a - "There were elections for governor in Mendoza last June 21.  Do you remember
 # who was the candidate who got the most votes?"
# P77b - "There were elections for governor in Rio Negro last June 14.  Do you remember
 # who was the candidate who got the most votes?"
# polknowcont - Number of correct answers to seven questions
# polknow - 0 = zero or one question right; 1 = two or three questions right; 
 # 3 = four or more questions right

# P89a_1 - "Returning to the theme of politics, in the past year did you talk
 # with anyone about politics?"
# talkpol - recode of P89a_1 - 1 = yes; 0 = no

# P40 - "With which party [do you identify]?"
# PJ - 0/1; "Peronismo" or "Frente para la Victoria" = 1; otherwise = 0
# UCR - 0/1; "Radicalismo" = 1; otherwise = 0
# partymatch - 0/1; if mayor in vignette in PJ and respondent is PJ or if mayor in 
 # vignette is UCR and respondent is UCR = 1; otherwise = 0
# opppartymatch - 0/1; if mayor in vignette in PJ and respondent is UCR or if mayor in 
 # vignette is UCR and respondent is PJ = 1; otherwise = 0
# nopartymatch - 0/1; if vignette names party of mayor but respondent identifies as
 # neither PJ not UCR
# nopartyvignette - 0/1; =1 if vignette=={1, 4, 7, or 10}; =0 otherwise


#############
# LOAD DATA #
#############

brazil.data <- readstata13::read.dta13("Weitz-ShapiroWinters_JOP_Credibility_ReplicationData.dta")
argentina.data <- readstata13::read.dta13("WintersWeitz-Shapiro_PSRM_Argentina_ReplicationData.dta")

##################################################
# CREATE A FUNCTION TO REPORT DESIRED STATISTICS #
##################################################

ttest <- function(dv, group){
  n.0 <- sum(is.na(dv[group==0])==0)
  n.1 <- sum(is.na(dv[group==1])==0)
  mean.0 <- mean(dv[group==0], na.rm=T)
  se.0 <- sd(dv[group==0], na.rm=T)/sqrt(n.0)
  mean.1 <- mean(dv[group==1], na.rm=T)
  se.1 <- sd(dv[group==1], na.rm=T)/sqrt(n.1)
  diff <- mean.0 - mean.1           # Less credible minus more credible
  se.diff <- sqrt(se.0^2 + se.1^2)
  p.val <- t.test(dv ~ group)$p.value
  wilcox.p <- wilcox.test(dv ~ group)$p.value
  cat("Mean for Group 0", "\t", round(mean.0, 2), "\n")
  cat("SE for Group 0", "\t", "\t", round(se.0, 2), "\n")
  cat("N for Group 0", "\t", "\t",round(n.0, 0), "\n")
  cat("Mean for Group 1", "\t", round(mean.1, 2), "\n")
  cat("SE for Group 1", "\t", "\t", round(se.1, 2), "\n")
  cat("N for Group 1", "\t", "\t", round(n.1, 0), "\n")
  cat("Difference", "\t", "\t", round(diff, 2), "\n")
  cat("SE of Difference", "\t", round(se.diff, 2), "\n")
  cat("p-value", "\t", "\t", round(p.val, 3), "\n")
  cat("Wilcox p-value", "\t", "\t", round(wilcox.p, 3), "\n")
}

#########################################################
# SET NUMBER OF SIMULATIONS FOR RANDOMIZATION INFERENCE #
#########################################################

n.sims <- 1000

###########
# TABLE 1 #
###########

attach(argentina.data)

#########################################
# Background Work for Comparison of CATEs

 # Estimated Average Treatment Effect for Credible Accusations versus Less Credible Accusations
ate.hat.cred.v.less <- mean(voteintent[cred_vs_less==1], na.rm=T) - 
  mean(voteintent[cred_vs_less==0], na.rm=T)

 # Create the Unobserved Potential Outcomes Based on the estimated ATE
y.i.0 <- ifelse(cred_vs_less==0, voteintent, voteintent - ate.hat.cred.v.less)
y.i.1 <- ifelse(cred_vs_less==1, voteintent, voteintent + ate.hat.cred.v.less)

 # Put Together Data
data.for.sims <- data.frame(cbind(y.i.0, y.i.1, education, polknow, talkpol))
colnames(data.for.sims) <- c("y.i.0", "y.i.1", "educ", "polknow", "talkpol")
 # Get rid of observations that were not involved in the credibility treatments
data.for.sims <- subset(data.for.sims, is.na(data.for.sims$y.i.0)==0)
 # Clean Up
rm(y.i.0); rm(y.i.1)

##########
# Column 1
ttest(voteintent, clean_vs_control)
ttest(voteintent, cred_vs_less)

#################
# Columns 2 and 3

ttest(voteintent[education < 3], clean_vs_control[education < 3])
ttest(voteintent[education < 3], cred_vs_less[education < 3])
ttest(voteintent[education==3], clean_vs_control[education==3])
ttest(voteintent[education==3], cred_vs_less[education==3])

 # Difference in CATEs
cate.hat.lowedu <- mean(voteintent[cred_vs_less==0 & education<3], na.rm=T) - 
  mean(voteintent[cred_vs_less==1 & education<3], na.rm=T)
cate.hat.highedu <- mean(voteintent[cred_vs_less==0 & education==3], na.rm=T) - 
  mean(voteintent[cred_vs_less==1 & education==3], na.rm=T)
diff.cates.edu <- cate.hat.highedu - cate.hat.lowedu
cat(diff.cates.edu)

 # RI Test for Difference in CATEs

  # Drop Observations with Missing Values on Education 
data.for.sims.edu <- subset(data.for.sims, is.na(data.for.sims$educ)==0)
  # Attach Data Frame
attach(data.for.sims.edu)
 # Collection Vector
diff.cates.educ.sims <- rep(NA, n.sims)
 # Run Loop
set.seed(2201978)
for (i in 1:n.sims) {
  t <- rbinom(n=length(y.i.0), size=1, prob=0.5)
  cate.high <- mean(y.i.0[t==1 & educ==3]) - mean(y.i.1[t==0 & educ==3])
  cate.low <- mean(y.i.0[t==1 & educ < 3]) - mean(y.i.1[t==0 & educ < 3])
  diff.cates.educ.sims[i] <- cate.high - cate.low
}
 # Test
mean(abs(diff.cates.educ.sims) >= abs(diff.cates.edu))
 # Clean Up
detach(data.for.sims.edu)

#################
# Columns 4 and 5

ttest(voteintent[polknow<2], clean_vs_control[polknow<2])
ttest(voteintent[polknow<2], cred_vs_less[polknow<2])
ttest(voteintent[polknow==2], clean_vs_control[polknow==2])
ttest(voteintent[polknow==2], cred_vs_less[polknow==2])

 # Difference in CATEs
cate.hat.lowknow <- mean(voteintent[cred_vs_less==0 & polknow < 2], na.rm=T) - 
  mean(voteintent[cred_vs_less==1 & polknow < 2], na.rm=T)
cate.hat.highknow <- mean(voteintent[cred_vs_less==0 & polknow==2], na.rm=T) - 
  mean(voteintent[cred_vs_less==1 & polknow==2], na.rm=T)
diff.cates.know <- cate.hat.highknow - cate.hat.lowknow
cat(diff.cates.know)

 # RI Test for Difference in CATEs

  # Drop Observations with Missing Values on Knowledge 
data.for.sims.know <- subset(data.for.sims, is.na(data.for.sims$polknow)==0)
  # Attach Data Frame
attach(data.for.sims.know)
  # Collection Vector
diff.cates.know.sims <- rep(NA, n.sims)
  # Run Loop
set.seed(431979)
for (i in 1:n.sims) {
  t <- rbinom(n=length(y.i.0), size=1, prob=0.5)
  cate.high <- mean(y.i.0[t==1 & polknow==2]) - mean(y.i.1[t==0 & polknow==2])
  cate.low <- mean(y.i.0[t==1 & polknow < 2]) - mean(y.i.1[t==0 & polknow < 2])
  diff.cates.know.sims[i] <- cate.high - cate.low
}
  # Test
mean(abs(diff.cates.know.sims) >= abs(diff.cates.know))
  # Clean Up
detach(data.for.sims.know)

#################
# Columns 6 and 7

ttest(voteintent[talkpol==0], clean_vs_control[talkpol==0])
ttest(voteintent[talkpol==0], cred_vs_less[talkpol==0])
ttest(voteintent[talkpol==1], clean_vs_control[talkpol==1])
ttest(voteintent[talkpol==1], cred_vs_less[talkpol==1])

 # Difference in CATEs
cate.hat.lowtalk <- mean(voteintent[cred_vs_less==0 & talkpol==0], na.rm=T) - 
  mean(voteintent[cred_vs_less==1 & talkpol==0], na.rm=T)
cate.hat.hightalk <- mean(voteintent[cred_vs_less==0 & talkpol==1], na.rm=T) - 
  mean(voteintent[cred_vs_less==1 & talkpol==1], na.rm=T)
diff.cates.talk <- cate.hat.hightalk - cate.hat.lowtalk
cat(diff.cates.talk)

 # RI Test for Difference in CATEs

  # Drop Observations with Missing Values on Knowledge 
data.for.sims.talkpol <- subset(data.for.sims, is.na(data.for.sims$talkpol)==0)
  # Attach Data Frame
attach(data.for.sims.talkpol)
  # Collection Vector
diff.cates.talk.sims <- rep(NA, n.sims)
  # Run Loop
set.seed(1231985)
for (i in 1:n.sims) {
  t <- rbinom(n=length(y.i.0), size=1, prob=0.5)
  cate.high <- mean(y.i.0[t==1 & talkpol==1]) - mean(y.i.1[t==0 & talkpol==1])
  cate.low <- mean(y.i.0[t==1 & talkpol==0]) - mean(y.i.1[t==0 & talkpol==0])
  diff.cates.talk.sims[i] <- cate.high - cate.low
}
  # Test
mean(abs(diff.cates.talk.sims) >= abs(diff.cates.talk))
  # Clean Up
detach(data.for.sims.talkpol)

##########
# Clean Up

detach(argentina.data)

############
# FIGURE 1 #
############

point.est.plot <- c(mean(brazil.data$voteintent[brazil.data$education < 4 & brazil.data$cred_vs_less==0], na.rm=T),
 mean(brazil.data$voteintent[brazil.data$education < 4 & brazil.data$cred_vs_less==1], na.rm=T),
 mean(brazil.data$voteintent[brazil.data$education==4 & brazil.data$cred_vs_less==0], na.rm=T),
 mean(brazil.data$voteintent[brazil.data$education==4 & brazil.data$cred_vs_less==1], na.rm=T),
 mean(argentina.data$voteintent[argentina.data$education < 3 & argentina.data$cred_vs_less==0], na.rm=T),
 mean(argentina.data$voteintent[argentina.data$education < 3 & argentina.data$cred_vs_less==1], na.rm=T),
 mean(argentina.data$voteintent[argentina.data$education==3 & argentina.data$cred_vs_less==0], na.rm=T),
 mean(argentina.data$voteintent[argentina.data$education==3 & argentina.data$cred_vs_less==1], na.rm=T))

se.est.plot <- c(sd(brazil.data$voteintent[brazil.data$education < 4 & brazil.data$cred_vs_less==0], na.rm=T)/
  sqrt(sum(is.na(brazil.data$voteintent[brazil.data$education < 4 & brazil.data$cred_vs_less==0])==0)),
 sd(brazil.data$voteintent[brazil.data$education < 4 & brazil.data$cred_vs_less==1], na.rm=T)/
  sqrt(sum(is.na(brazil.data$voteintent[brazil.data$education < 4 & brazil.data$cred_vs_less==1])==0)),
 sd(brazil.data$voteintent[brazil.data$education==4 & brazil.data$cred_vs_less==0], na.rm=T)/
  sqrt(sum(is.na(brazil.data$voteintent[brazil.data$education==4 & brazil.data$cred_vs_less==0])==0)),
 sd(brazil.data$voteintent[brazil.data$education==4 & brazil.data$cred_vs_less==1], na.rm=T)/
  sqrt(sum(is.na(brazil.data$voteintent[brazil.data$education==4 & brazil.data$cred_vs_less==1])==0)),
 sd(argentina.data$voteintent[argentina.data$education < 3 & argentina.data$cred_vs_less==0], na.rm=T)/
  sqrt(sum(is.na(argentina.data$voteintent[argentina.data$education < 3 & argentina.data$cred_vs_less==0])==0)),
 sd(argentina.data$voteintent[argentina.data$education < 3 & argentina.data$cred_vs_less==1], na.rm=T)/
  sqrt(sum(is.na(argentina.data$voteintent[argentina.data$education < 3 & argentina.data$cred_vs_less==1])==0)),
 sd(argentina.data$voteintent[argentina.data$education==3 & argentina.data$cred_vs_less==0], na.rm=T)/
  sqrt(sum(is.na(argentina.data$voteintent[argentina.data$education==3 & argentina.data$cred_vs_less==0])==0)),
 sd(argentina.data$voteintent[argentina.data$education==3 & argentina.data$cred_vs_less==1], na.rm=T)/
  sqrt(sum(is.na(argentina.data$voteintent[argentina.data$education==3 & argentina.data$cred_vs_less==1])==0)))

x.points <- c(1, 1.25, 1.75, 2, 2.5, 2.75, 3.25, 3.5)

plot(x.points[c(1,3,5,7)], point.est.plot[c(1,3,5,7)], ylim=c(1.5, 3.5), pch=19, xlim=c(x.points[1]-0.25, x.points[8]+0.25), 
 axes=F, frame.plot=T, xlab="", ylab="Average Vote Intention", main="Varying Reactions to Credibility by Education")
segments (x.points, point.est.plot + 1.96*se.est.plot, x.points, point.est.plot - 1.96*se.est.plot)
points(x.points[c(2,4,6,8)], point.est.plot[c(2,4,6,8)], pch=1)
abline(v=2.25, lty=3)
axis(2)
axis(1, at=c(sum(x.points[1:2])/2, sum(x.points[3:4])/2, sum(x.points[5:6])/2, sum(x.points[7:8])/2),
 labels=c("Completed\nHigh School\nor More", "Some Tertiary\nEducation\nor More", "Completed\nHigh School\nor Less", "Some Tertiary\nEducation\nor More"),
 padj=0.75)
text(1.5, 3.4, "Brazil")
text(3, 3.4, "Argentina")
legend(x=0.75, y=3.25, pch=c(19,1), legend=c("Less Credible", "More Credible"))

###########
# TABLE 2 # 
###########

# Note: Calculation of p-values for differences in CATEs are grouped together at the end

attach(argentina.data)

##############
# First Column 

ttest(voteintent[partymatch==1], clean_vs_control[partymatch==1])
ttest(voteintent[partymatch==1], cred_vs_less[partymatch==1])

##############
# Second Column 

ttest(voteintent[opppartymatch==1], clean_vs_control[opppartymatch==1])
ttest(voteintent[opppartymatch==1], cred_vs_less[opppartymatch==1])

##############
# Third Column 

ttest(voteintent[nopartymatch==1], clean_vs_control[nopartymatch==1])
ttest(voteintent[nopartymatch==1], cred_vs_less[nopartymatch==1])

##############
# Fourth Column 

ttest(voteintent[nopartyvignette==1], clean_vs_control[nopartyvignette==1])
ttest(voteintent[nopartyvignette==1], cred_vs_less[nopartyvignette==1])

##############################################
# Background Statistics for Partisanship CATEs

 # Estimated Average Treatment Effect for Credible Accusations versus Less Credible Accusations
ate.hat.cred.v.less <- mean(voteintent[cred_vs_less==1], na.rm=T) - 
  mean(voteintent[cred_vs_less==0], na.rm=T)

 # Create the Unobserved Potential Outcomes Based on the estimated ATE
y.i.0 <- ifelse(cred_vs_less==0, voteintent, voteintent - ate.hat.cred.v.less)
y.i.1 <- ifelse(cred_vs_less==1, voteintent, voteintent + ate.hat.cred.v.less)

 # Put Together Data
data.for.sims <- data.frame(cbind(y.i.0, y.i.1, partymatch, opppartymatch, nopartymatch, nopartyvignette))
colnames(data.for.sims) <- c("y.i.0", "y.i.1", "partymatch", "opppartymatch", "nopartymatch", "nopartyvignette")
 # Get rid of observations that were not involved in the credibility treatments
data.for.sims <- subset(data.for.sims, is.na(data.for.sims$y.i.0)==0)
 # Clean Up
rm(y.i.0); rm(y.i.1)

 # Estimated CATEs, Differences, and Vectors for Simulations
cate.hat.partymatch <- mean(voteintent[cred_vs_less==1 & partymatch==1], na.rm=T) - 
  mean(voteintent[cred_vs_less==0 & partymatch==1], na.rm=T)
cate.hat.opppartymatch <- mean(voteintent[cred_vs_less==1 & opppartymatch==1], na.rm=T) - 
  mean(voteintent[cred_vs_less==0 & opppartymatch==1], na.rm=T)
cate.hat.nopartymatch <- mean(voteintent[cred_vs_less==1 & nopartymatch==1], na.rm=T) - 
  mean(voteintent[cred_vs_less==0 & nopartymatch==1], na.rm=T)
cate.hat.nopartyvignette <- mean(voteintent[cred_vs_less==1 & nopartyvignette==1], na.rm=T) - 
  mean(voteintent[cred_vs_less==0 & nopartyvignette==1], na.rm=T)

diff.cates.opppartymatch <- cate.hat.partymatch - cate.hat.opppartymatch
diff.cates.nopartymatch <- cate.hat.partymatch - cate.hat.nopartymatch
diff.cates.nopartyvignette <- cate.hat.partymatch - cate.hat.nopartyvignette

cat(diff.cates.opppartymatch, diff.cates.nopartymatch, diff.cates.nopartyvignette)

detach(argentina.data)

###################
# CATEs Comparison

 # Attach Data Frame
attach(data.for.sims)
 # Collection Vectors
diff.cates.opppartymatch.sims <- rep(NA, n.sims)
diff.cates.nopartymatch.sims <- rep(NA, n.sims)
diff.cates.nopartyvignette.sims <- rep(NA, n.sims)
 # Run Loop
set.seed(2201978)
for (i in 1:n.sims) {
  t <- rbinom(n=length(y.i.0), size=1, prob=0.5)
  cate.partymatch <- mean(y.i.0[t==1 & partymatch==1]) - mean(y.i.1[t==0 & partymatch==1])
  cate.opppartymatch <- mean(y.i.0[t==1 & opppartymatch==1]) - mean(y.i.1[t==0 & opppartymatch==1])
  cate.nopartymatch <- mean(y.i.0[t==1 & nopartymatch==1], na.rm=T) - mean(y.i.1[t==0 & nopartymatch==1], na.rm=T)
  cate.nopartyvignette <- mean(y.i.0[t==1 & nopartyvignette==1]) - mean(y.i.1[t==0 & nopartyvignette==1])
  diff.cates.opppartymatch.sims[i] <- cate.partymatch - cate.opppartymatch
  diff.cates.nopartymatch.sims[i] <- cate.partymatch - cate.nopartymatch
  diff.cates.nopartyvignette.sims[i] <- cate.partymatch - cate.nopartyvignette
}
 # Tests
mean(abs(diff.cates.opppartymatch.sims) >= abs(diff.cates.opppartymatch))
mean(abs(diff.cates.nopartymatch.sims) >= abs(diff.cates.nopartymatch))
mean(abs(diff.cates.nopartyvignette.sims) >= abs(diff.cates.nopartyvignette))
 # Clean Up
detach(data.for.sims)

############
# FIGURE 2 #
############

point.est.plot.party <- c(mean(argentina.data$voteintent[argentina.data$partymatch==1 & argentina.data$control==1], na.rm=T),
 mean(argentina.data$voteintent[argentina.data$partymatch==1 & argentina.data$clean==1], na.rm=T),
 mean(argentina.data$voteintent[argentina.data$partymatch==1 & argentina.data$bribes_less==1], na.rm=T),
 mean(argentina.data$voteintent[argentina.data$partymatch==1 & argentina.data$bribes_credible==1], na.rm=T),
 mean(argentina.data$voteintent[argentina.data$opppartymatch==1 & argentina.data$control==1], na.rm=T),
 mean(argentina.data$voteintent[argentina.data$opppartymatch==1 & argentina.data$clean==1], na.rm=T),
 mean(argentina.data$voteintent[argentina.data$opppartymatch==1 & argentina.data$bribes_less==1], na.rm=T),
 mean(argentina.data$voteintent[argentina.data$opppartymatch==1 & argentina.data$bribes_credible==1], na.rm=T))

se.est.plot.party <- c(sd(argentina.data$voteintent[argentina.data$partymatch==1 & argentina.data$control==1], na.rm=T)/
  sqrt(sum(is.na(argentina.data$voteintent[argentina.data$partymatch==1 & argentina.data$control==1])==0)),
 sd(argentina.data$voteintent[argentina.data$partymatch==1 & argentina.data$clean==1], na.rm=T)/
  sqrt(sum(is.na(argentina.data$voteintent[argentina.data$partymatch==1 & argentina.data$clean==1])==0)),
 sd(argentina.data$voteintent[argentina.data$partymatch==1 & argentina.data$bribes_less==1], na.rm=T)/
   sqrt(sum(is.na(argentina.data$voteintent[argentina.data$partymatch==1 & argentina.data$bribes_less==1])==0)),
 sd(argentina.data$voteintent[argentina.data$partymatch==1 & argentina.data$bribes_less==1], na.rm=T)/
   sqrt(sum(is.na(argentina.data$voteintent[argentina.data$partymatch==1 & argentina.data$bribes_less==1])==0)),
 sd(argentina.data$voteintent[argentina.data$opppartymatch==1 & argentina.data$control==1], na.rm=T)/
   sqrt(sum(is.na(argentina.data$voteintent[argentina.data$opppartymatch==1 & argentina.data$control==1])==0)),
 sd(argentina.data$voteintent[argentina.data$opppartymatch==1 & argentina.data$clean==1], na.rm=T)/
   sqrt(sum(is.na(argentina.data$voteintent[argentina.data$opppartymatch==1 & argentina.data$clean==1])==0)),
 sd(argentina.data$voteintent[argentina.data$opppartymatch==1 & argentina.data$bribes_less==1], na.rm=T)/
   sqrt(sum(is.na(argentina.data$voteintent[argentina.data$opppartymatch==1 & argentina.data$bribes_less==1])==0)),
 sd(argentina.data$voteintent[argentina.data$opppartymatch==1 & argentina.data$bribes_less==1], na.rm=T)/
   sqrt(sum(is.na(argentina.data$voteintent[argentina.data$opppartymatch==1 & argentina.data$bribes_less==1])==0)))

x.points.party <- c(1, 2, 3, 4, 6, 7, 8, 9)

par(mar=c(2, 4, 4, 2)+0.1)

plot(x.points.party, point.est.plot.party, pch=c(19, 15, 21, 22), ylim=c(1,4), axes=F, frame.plot=T, xlab="", 
 xlim=c(x.points.party[1]-0.5, x.points.party[8]+0.5),
 ylab="Average Vote Intention", main="Vote Intention by Treatment for Copartisans and Opposition Partisans")
segments (x.points.party, point.est.plot.party + 1.96*se.est.plot.party, 
 x.points.party, point.est.plot.party - 1.96*se.est.plot.party)
abline(v=5, lty=3)
axis(2)
text(2.5, 4, "Mayoral Copartisans")
text(7.5, 4, "Opposition Partisans")
legend(x=0.5, y=2, pch=c(19,15,21,22), legend=c("Control", "Clean", "Less Credible", "More Credible"))

###################
# ONLINE APPENDIX #
###################

####################
# APPENDIX TABLE 1 #
####################

attach(brazil.data)

# Note: Test statistics for all CATEs are estimated at the end

##########
# Column 1

ttest(voteintent, clean_vs_pure)
ttest(voteintent, cred_vs_less)

#################
# Columns 2 and 3

ttest(voteintent[education < 4], clean_vs_pure[education < 4])
ttest(voteintent[education < 4], cred_vs_less[education < 4])

ttest(voteintent[education==4], clean_vs_pure[education==4])
ttest(voteintent[education==4], cred_vs_less[education==4])

#################
# Columns 4 and 5

ttest(voteintent[polknowledge<2], clean_vs_pure[polknowledge<2])
ttest(voteintent[polknowledge<2], cred_vs_less[polknowledge<2])

ttest(voteintent[polknowledge==2], clean_vs_pure[polknowledge==2])
ttest(voteintent[polknowledge==2], cred_vs_less[polknowledge==2])

#################
# Columns 6 and 7

ttest(voteintent[talkpol<3], clean_vs_pure[talkpol<3])
ttest(voteintent[talkpol<3], cred_vs_less[talkpol<3])

ttest(voteintent[talkpol==3], clean_vs_pure[talkpol==3])
ttest(voteintent[talkpol==3], cred_vs_less[talkpol==3])

#################################
# Background Statistics for CATEs

 # Estimated Average Treatment Effect for Credible Accusations versus Less Credible Accusations
ate.hat.cred.v.less <- mean(voteintent[cred_vs_less==1], na.rm=T) - 
  mean(voteintent[cred_vs_less==0], na.rm=T)

 # Create the Unobserved Potential Outcomes Based on the estimated ATE
y.i.0 <- ifelse(cred_vs_less==0, voteintent, voteintent - ate.hat.cred.v.less)
y.i.1 <- ifelse(cred_vs_less==1, voteintent, voteintent + ate.hat.cred.v.less)

 # Put Together Data
data.for.sims <- data.frame(cbind(y.i.0, y.i.1, education, polknowledge, talkpol))
colnames(data.for.sims) <- c("y.i.0", "y.i.1", "educ", "polknow", "talkpol")
 # Get rid of observations that were not involved in the credibility treatments
data.for.sims <- subset(data.for.sims, is.na(data.for.sims$y.i.0)==0)
 # Clean Up
rm(y.i.0); rm(y.i.1)

 # Estimated CATEs, Differences, and Vectors for Simulations
cate.hat.lowedu <- mean(voteintent[cred_vs_less==0 & education<4], na.rm=T) - 
  mean(voteintent[cred_vs_less==1 & education<4], na.rm=T)
cate.hat.highedu <- mean(voteintent[cred_vs_less==0 & education==4], na.rm=T) - 
  mean(voteintent[cred_vs_less==1 & education==4], na.rm=T)
diff.cates.edu <- cate.hat.highedu - cate.hat.lowedu

cate.hat.lowknow <- mean(voteintent[cred_vs_less==0 & polknowledge < 2], na.rm=T) - 
  mean(voteintent[cred_vs_less==1 & polknowledge < 2], na.rm=T)
cate.hat.highknow <- mean(voteintent[cred_vs_less==0 & polknowledge==2], na.rm=T) - 
  mean(voteintent[cred_vs_less==1 & polknowledge==2], na.rm=T)
diff.cates.know <- cate.hat.highknow - cate.hat.lowknow

cate.hat.lowtalk <- mean(voteintent[cred_vs_less==0 & talkpol < 3], na.rm=T) - 
  mean(voteintent[cred_vs_less==1 & talkpol < 3], na.rm=T)
cate.hat.hightalk <- mean(voteintent[cred_vs_less==0 & talkpol==3], na.rm=T) - 
  mean(voteintent[cred_vs_less==1 & talkpol==3], na.rm=T)
diff.cates.talk <- cate.hat.hightalk - cate.hat.lowtalk

cat(diff.cates.edu, diff.cates.know, diff.cates.talk)

detach(brazil.data)

############################
# Education CATEs Comparison

 # Drop Observations with Missing Values on Education 
data.for.sims.edu <- subset(data.for.sims, is.na(data.for.sims$educ)==0)
 # Attach Data Frame
attach(data.for.sims.edu)
 # Collection Vector
diff.cates.educ.sims <- rep(NA, n.sims)
 # Run Loop
set.seed(2201978)
for (i in 1:n.sims) {
  t <- rbinom(n=length(y.i.0), size=1, prob=0.5)
  cate.high <- mean(y.i.0[t==1 & educ==4]) - mean(y.i.1[t==0 & educ==4])
  cate.low <- mean(y.i.0[t==1 & educ < 4]) - mean(y.i.1[t==0 & educ < 4])
  diff.cates.educ.sims[i] <- cate.high - cate.low
}
 # Test
mean(abs(diff.cates.educ.sims) >= abs(diff.cates.edu))
 # Clean Up
detach(data.for.sims.edu)

############################
# Knowledge CATEs Comparison

 # Drop Observations with Missing Values on Knowledge 
data.for.sims.know <- subset(data.for.sims, is.na(data.for.sims$polknow)==0)
 # Attach Data Frame
attach(data.for.sims.know)
 # Collection Vector
diff.cates.know.sims <- rep(NA, n.sims)
 # Run Loop
set.seed(431979)
for (i in 1:n.sims) {
  t <- rbinom(n=length(y.i.0), size=1, prob=0.5)
  cate.high <- mean(y.i.0[t==1 & polknow==2]) - mean(y.i.1[t==0 & polknow==2])
  cate.low <- mean(y.i.0[t==1 & polknow < 2]) - mean(y.i.1[t==0 & polknow < 2])
  diff.cates.know.sims[i] <- cate.high - cate.low
}
 # Test
mean(abs(diff.cates.know.sims) >= abs(diff.cates.know))
 # Clean Up
detach(data.for.sims.know)

############################
# Discussion CATEs Comparison

 # Drop Observations with Missing Values on Knowledge 
data.for.sims.talkpol <- subset(data.for.sims, is.na(data.for.sims$talkpol)==0)
 # Attach Data Frame
attach(data.for.sims.talkpol)
 # Collection Vector
diff.cates.talk.sims <- rep(NA, n.sims)
 # Run Loop
set.seed(1231985)
for (i in 1:n.sims) {
  t <- rbinom(n=length(y.i.0), size=1, prob=0.5)
  cate.high <- mean(y.i.0[t==1 & talkpol==3]) - mean(y.i.1[t==0 & talkpol==3])
  cate.low <- mean(y.i.0[t==1 & talkpol < 3]) - mean(y.i.1[t==0 & talkpol < 3])
  diff.cates.talk.sims[i] <- cate.high - cate.low
}
 # Test
mean(abs(diff.cates.talk.sims) >= abs(diff.cates.talk))
 # Clean Up
detach(data.for.sims.talkpol)


####################
# APPENDIX TABLE 2 #
####################

attach(argentina.data)

##########
# Column 1
ttest(votedich, clean_vs_control)
ttest(votedich, cred_vs_less)

#################
# Columns 2 and 3

ttest(votedich[education < 3], clean_vs_control[education < 3])
ttest(votedich[education < 3], cred_vs_less[education < 3])
ttest(votedich[education==3], clean_vs_control[education==3])
ttest(votedich[education==3], cred_vs_less[education==3])

#################
# Columns 4 and 5

ttest(votedich[polknow<2], clean_vs_control[polknow<2])
ttest(votedich[polknow<2], cred_vs_less[polknow<2])
ttest(votedich[polknow==2], clean_vs_control[polknow==2])
ttest(votedich[polknow==2], cred_vs_less[polknow==2])

#################
# Columns 6 and 7

ttest(votedich[talkpol==0], clean_vs_control[talkpol==0])
ttest(votedich[talkpol==0], cred_vs_less[talkpol==0])
ttest(votedich[talkpol==1], clean_vs_control[talkpol==1])
ttest(votedich[talkpol==1], cred_vs_less[talkpol==1])

####################
# APPENDIX TABLE 3 #
####################

##############
# First Column 

ttest(votedich[partymatch==1], clean_vs_control[partymatch==1])
ttest(votedich[partymatch==1], cred_vs_less[partymatch==1])

##############
# Second Column 

ttest(votedich[opppartymatch==1], clean_vs_control[opppartymatch==1])
ttest(votedich[opppartymatch==1], cred_vs_less[opppartymatch==1])

##############
# Third Column 

ttest(votedich[nopartymatch==1], clean_vs_control[nopartymatch==1])
ttest(votedich[nopartymatch==1], cred_vs_less[nopartymatch==1])

##############
# Fourth Column 

ttest(votedich[nopartyvignette==1], clean_vs_control[nopartyvignette==1])
ttest(votedich[nopartyvignette==1], cred_vs_less[nopartyvignette==1])



# End of File 